package com.whiteblue.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.whiteblue.interceptor.AdminInterceptor;
import com.whiteblue.interceptor.LoginInterceptor;
import com.whiteblue.model.Reply;
import com.whiteblue.validator.ReplyValidator;

/**
 * Created with IntelliJ IDEA.
 */
public class ReplyController extends Controller {
	public void index() {
		if (0 == getParaToInt(1, 1)) {
			setAttr("replyPage", Reply.dao.getLastPage(getParaToInt(0)));
		} else {
			setAttr("replyPage",
					Reply.dao.getPage(getParaToInt(0), getParaToInt(1, 1)));
		}
		setAttr("postID", getParaToInt(0));
		render("/reply/_reply.html");
	}

	@Before({ LoginInterceptor.class, ReplyValidator.class })
	public void save() {
		Reply reply = getModel(Reply.class).set("userID",
				getSessionAttr("userID"));
		int postID = reply.getInt("postID");
		reply.mySave(postID);
		forwardAction("/reply/" + postID + "-0");
	}

	/* ----------------------admin---------------------- */

	@Before(AdminInterceptor.class)
	public void delete() {
		Reply.dao.deleteByID(getParaToInt(0));
		forwardAction("/admin/replyList/" + getParaToInt(1));
	}
}
